package com.meng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.meng.dao.*;
import com.meng.pojo.*;
import com.meng.service.UserService;
import com.meng.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TUserMapper tUserMapper;

    @Autowired
    private TRoleMapper tRoleMapper;

    @Autowired
    private TUserRoleMapper tuserrolemapper;
    
    @Autowired
    private TRolePermissionMapper trolepermissionmapper;
    
    @Autowired
    private TPermissionMapper tpermissionmapper;

    //通过用户名查找用户对象
    @Override
    public TUser queryUsernamebyName(String username) {
        return tUserMapper.queryUsernamebyName(username);
    }


    @Override
    public ResultUtil selUser(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        TUserExample example = new TUserExample();
        List<TUser> list = tUserMapper.selectByExample(example);
        // 将roleName写进TUser
        for (TUser tuser:list) {
            List<TRole> roles = selRoles();
            for (TRole trole:roles) {
                if (trole.getRoleId()==tuser.getUserId()){
                    System.out.println("角色名称：------------->"+trole.getName());
                    tuser.setRemark(trole.getName());
                }
            }
        }
        PageInfo<TUser> pageInfo = new PageInfo<TUser>(list);
        ResultUtil resultUtil = new ResultUtil();
        resultUtil.setCode(0);
        resultUtil.setCount(pageInfo.getTotal());
        resultUtil.setData(pageInfo.getList());
        return resultUtil;
    }


    //获取角色
    @Override
    public List<TRole> selRoles() {

        TRoleExample example = new TRoleExample();

        List<TRole> list = tRoleMapper.selectByExample(example);

        return list;
    }


    //通过userid查找角色id
	@Override
	public List<Integer> queryrolesid(int userid) {
		return tuserrolemapper.selectRolesByUserId(userid);
	}

	//通过角色id查找权限id
	@Override
	public List<Integer> querypermissoinsid(int roleid) {
		return trolepermissionmapper.querypermissionsByrolesid(roleid);
	}

	//通过权限id查找权限名称
	@Override
	public TPermission querypermission(int permissionid) {
		return tpermissionmapper.selectByPrimaryKey(permissionid);
	}
}

